//
//  EnterExitRegionLogViewController.m
//  location
//
//  Created by Dongxiang Xu on 2025/10/14.
//

#import "EnterExitRegionLogViewController.h"

@interface EnterExitRegionLogViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *regionLog;

@end

@implementation EnterExitRegionLogViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = RealWhiteColor;
    [self initView];
}

- (void)initView
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentsDirectory = paths.firstObject;
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"regionLog.plist"];
    NSFileManager *fileManager = [[NSFileManager  alloc] init];
    if(![fileManager fileExistsAtPath:plistPath]){
        [fileManager createFileAtPath:plistPath contents:nil attributes:nil];
    }
  
    self.regionLog = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    if (!self.regionLog) {
        self.regionLog = [NSMutableArray array];
    }
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, STATUSHEIGHT, SCREEN_WIDTH, 44)];
    title.font = MEDIUM_FONT(18);
    title.textAlignment = NSTextAlignmentCenter;
    title.text = @"进出围栏记录";
    title.textColor = RealBlackColor;
    [self.view addSubview:title];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVHEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-NAVHEIGHT) style:UITableViewStyleGrouped];
    tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    tableView.estimatedRowHeight = 0;
    tableView.estimatedSectionHeaderHeight = 0;
    tableView.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    if (@available(iOS 15.0, *)) {
        tableView.fillerRowHeight = 0;
        tableView.sectionHeaderTopPadding = 0;
    }
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.backgroundColor = RealWhiteColor;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
    UIButton *delete = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [delete setTitle:@"删除" forState:UIControlStateNormal];
    [delete.titleLabel setFont:DEFAULT_FONT(12)];
    [delete setTitleColor:RealBlackColor forState:UIControlStateNormal];
    [[delete rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定清除所有记录？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self.regionLog removeAllObjects];
            [self.regionLog writeToFile:plistPath atomically:YES];
            [tableView reloadData];
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:action1];
        [alertController addAction:action2];
        [self presentViewController:alertController animated:YES completion:nil];
    }];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:delete];
    self.navigationItem.rightBarButtonItem = rightItem;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.regionLog.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"时间：%@  %@",self.regionLog[indexPath.row][@"time"],self.regionLog[indexPath.row][@"event"]];
    cell.textLabel.font = DEFAULT_FONT(10);
    return cell;
}

@end
